package com.newbye.component
{
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.text.TextField;
	import flash.utils.Dictionary;
	import flash.utils.describeType;
	import flash.utils.getDefinitionByName;
	
	import mx.core.UIComponent;

	public class ComponentInstanceItem extends UIComponent
	{
		
		private var _attrs:Dictionary = new Dictionary();
		
		private var _className:String;
		
		private var _componentInstance:DisplayObject;
		
		private var _variableName:String = "";
		/**
		 * 组件全名 
		 * @param className
		 * 
		 */		
		public function ComponentInstanceItem(className:String, componentInstance:DisplayObject)
		{
			super();
			this.mouseChildren = false;
			
			this.width = componentInstance.width;
			this.height = componentInstance.height;
			
			_className = className;
			
			_componentInstance = componentInstance;
			this.addChild(_componentInstance);
			var attXml:XML = flash.utils.describeType(_componentInstance);
			var strAttrs:* = attXml.accessor.(@type=="String");
		}
		/**
		 * 变量名 
		 * @return 
		 * 
		 */		
		public function get variableName():String
		{
			return _variableName;
		}

		public function set variableName(value:String):void
		{
			_variableName = value;
		}

		public function addAttr(attr:String, value:*):void
		{
			_attrs[attr] = value;
//			_componentInstance[attr] = value;
			refreshContainer();
		}
		
		public function removeAttr(attr:String):void
		{
			delete _attrs[attr];
		}
		
		public function getAttrValue(attr:String):*
		{
			return _attrs[attr];
		}
		
		public function get attrs():Dictionary
		{
			return this._attrs;
		}
		
		public function displayObject():DisplayObject
		{
			return this._componentInstance;
		}
		
		private function refreshContainer():void
		{
			if(this._attrs["width"])
			{
				this.width = this._attrs["width"];
			}
			if(this._attrs["height"])
			{
				this.height = this._attrs["height"];
			}
			
			if(this._attrs["y"])
			{
				this.y = this._attrs["y"];
			}
			if(this._attrs["x"])
			{
				this.x = this._attrs["x"];
			}
			var gw:Number = 100;
			var gh:Number = 100;
			if(this.width > 0 && this.height > 0)
			{
				gw = width;
				gh = height;
				
				this.graphics.beginFill(0x111111, 0.2);
				this.graphics.drawRect(0, 0, gw, gh);
				this.graphics.endFill();
			}
			else
			{
				var bmd:BitmapData = new BitmapData(gw, gh);
				var tf:TextField = new TextField();
				tf.text = this._className;
				bmd.draw(tf);
				this.graphics.beginBitmapFill(bmd);
				this.graphics.drawRect(0, 0, gw, gh);
				this.graphics.endFill();
			}
			
		}
	}
}